001    /*
002     * Copyright 2005 Stephen J. McConnell.
003     *
004     * Licensed  under the  Apache License,  Version 2.0  (the "License");
005     * you may not use  this file  except in  compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *   http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed  under the  License is distributed on an "AS IS" BASIS,
012     * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
013     * implied.
014     *
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    
019    package net.dpml.lang;
020    
021    import java.io.Serializable;
022    
023    import java.util.Random;
024    
025    /**
026     * The PID class is a process identifer. 
027     *
028     * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
029     * @version 1.0.1
030     */
031    public class PID implements Serializable
032    {
033        private final int m_value;
034    
035       /**
036        * Creation of a new process identifier.
037        */
038        public PID()
039        {
040            m_value = setupInitialValue();
041        }
042    
043       /**
044        * Creation of a new process identifier.
045        */
046        private PID( int id )
047        {
048            m_value = id;
049        }
050    
051        private static int setupInitialValue()
052        {
053            String id = System.getProperty( "process.id" );
054            if( null == id )
055            {
056                return Math.abs( new Random().nextInt() );
057            }
058            else
059            {
060                try
061                {
062                    return Integer.parseInt( id );
063                }
064                catch( NumberFormatException e )
065                {
066                    return Math.abs( new Random().nextInt() );
067                }
068            }
069        }
070    
071       /**
072        * Return the process identifier int value.
073        * @return the process id value
074        */
075        public int getValue()
076        {
077            return m_value;
078        }
079    
080       /**
081        * Return the string representation of this process identifier.
082        * @return the process identifier as a string
083        */
084        public String toString()
085        {
086            return "[" + m_value + "]";
087        }
088    
089       /**
090        * Test if a supplied object is equal to this process identifier.
091        * @param other the object to compare with this object
092        * @return TRUE if the objects are equivalent
093        */
094        public boolean equals( Object other )
095        {
096            if( other instanceof PID )
097            {
098                PID pid = (PID) other;
099                return getValue() == pid.getValue();
100            }
101            else
102            {
103               return false;
104            }
105        }
106    
107       /**
108        * Return the hashcode for this PID instance.
109        * @return the hashcode value
110        */
111        public int hashCode()
112        {
113            return getValue();
114        }
115    }
116